#include "stdafx.h"
#include "qhyperlink.h"


using namespace micro::gui;

QHyperlink::QHyperlink(void):
		m_bUrlVisited(false), 
		m_bClicked(false), 
		m_crVisited(RGB(128, 0, 128)),
        m_crNotVisited(RGB(0,0,255)), 
		m_hUrlFont(nullptr)
{
	

	// Create the cursor
	m_hCursor = ::LoadCursor(nullptr, IDC_HAND);
}


QHyperlink::~QHyperlink(void)
{
	if (m_hUrlFont)  ::DeleteObject(m_hUrlFont);
}

bool QHyperlink::AttachDlgItem(UINT nID, QWindow* pParent)
{
	HWND hwnd = pParent->GetDlgItem(nID);
	Attach(hwnd);
	SubclassWindow();


	LOGFONT lf;
	m_hUrlFont = (HFONT)::SendMessage( _window, WM_GETFONT, 0, 0);
	::GetObject(m_hUrlFont, sizeof(LOGFONT), &lf);
	lf.lfUnderline = TRUE;
	m_hUrlFont = ::CreateFontIndirect(&lf);

	return true;
}


void QHyperlink::OpenUrl()
{
	TCHAR szUrl[ MAX_PATH ] = _T("http://www.yonan.ro/lab/");

	if( (int)(LRESULT)::ShellExecute(nullptr, _T("open"), szUrl, nullptr, nullptr, SW_SHOWNORMAL ) > 32)
	{
		m_bUrlVisited = true;

		// redraw the window to update the color
		Invalidate( false);
	}
	//else
	//	DebugWarnMsg(_T("ShellExecute Failed"));
}


bool QHyperlink::OnLButtonDown(UINT flags, QPoint& point)
{
	SetCapture();
	m_bClicked = true;

	return true;
}
bool QHyperlink::OnLButtonUp(UINT flags, QPoint& point)
{
	ReleaseCapture();
	if(m_bClicked)
	{
		m_bClicked = false;
		 
		QRect rc;
		ClientToScreen( point);
		
		rc = GetWindowRect();

		if(rc.PtInRect( point))
			OpenUrl();
	}

	return true;

}